from toee import *
from utilities import *
from council import council_time

#Script contains:
# -Hommlet: DH spawns, Council spawns (maps 5001 - Hommlet exterior, 5048 - Town Hall, 5049 - Stonemason)
# -Moathouse: Moathouse respawns
# -delayed DH spawn in Town Hall until after council meeting

def san_first_heartbeat( attachee, triggerer ):
	if ( (game.global_flags[110] == 1 or game.global_flags[94] == 1 or game.story_state >= 5) and (attachee.map == 5002 or attachee.map == 5004 or attachee.map == 5005) ):
		Moathouse_Respawn(attachee, triggerer)
		return RUN_DEFAULT
	elif game.global_flags[699] == 1:
		DH_Spawner(attachee, triggerer)
	if ( (attachee.map == 5001 or attachee.map == 5048 or attachee.map == 5049) and get_v(435) != 0):
		Council_Script(attachee, triggerer)
	return RUN_DEFAULT

def san_heartbeat( attachee, triggerer ):
	c_time = council_time()
	if ( attachee.map == 5048 and get_v(435) != 0 and c_time == 1):
	# Townhall
		if get_v(435) == 1:
			set_v(435,3)
			Council_Script(attachee, triggerer)
	if (attachee.map == 5048):
	# Townhall
		if (get_v(436) == 3 and get_v(435) < 4):
		#you've found the trap, initiate dialogue with rufus to GTFO
			for npc in game.obj_list_vicinity(attachee.location,OLC_NPC):
				if (npc.name == 14006 or npc.name == 20002):
					game.leader.begin_dialog(npc,2000)
	return RUN_DEFAULT

def DH_Spawner(attachee, triggerer):
	if game.global_flags[698] == 0 and attachee.map == 5115:
		game.obj_create( 14698, location_from_axis (475L, 469L) )
		game.global_flags[698] = 1
	elif game.global_vars[698] ==0 and attachee.map == 5029:
		game.global_vars[698] = 1
		attachee.destroy()
	elif (attachee.map == 5120 and game.global_flags[695] == 0):
		target = game.obj_create( 14699, location_from_axis (485L, 483L))
		target.turn_towards(game.party[0])
		game.global_flags[695] = 1
	elif (attachee.map == 5006 and game.global_vars[695] == 1):
		game.obj_create( 14397, location_from_axis (474L, 474L))
		game.obj_create( 14397, location_from_axis (474L, 488L))
		game.global_vars[695] = 2
	elif (attachee.map == 5008 and game.global_vars[695] == 3 and not( is_daytime() )):
		game.obj_create( 14695, location_from_axis (472L, 477L))
		game.global_vars[695] = 4
	elif (attachee.map == 5048 and game.global_flags[690] == 1 and (get_v(435) ==0 or get_v(435) >= 4)):
		target = game.obj_create( 14689, location_from_axis (478L, 470L))
		target.turn_towards(game.party[0])
		target = game.obj_create( 14690, location_from_axis (473L, 472L))
		target.turn_towards(game.party[0])
		target = game.obj_create( 14691, location_from_axis (474L, 479L))
		target.turn_towards(game.party[0])
		target = game.obj_create( 14692, location_from_axis (478L, 479L))
		target.turn_towards(game.party[0])
		target = game.obj_create( 14693, location_from_axis (476L, 477L))
		target.turn_towards(game.party[0])
		game.global_flags[690] = 0
	return

def Moathouse_Respawn(attachee, triggerer):
	#if (attachee.map == 5002): # Moathouse Exterior
	#	# Create Bears and Boars
	#	bear1 = game.obj_create( 14052, location_from_axis (429L, 437L) )
	#	bear1.rotation = 2
	#	bear1.npc_flag_set( ONF_KOS )
	#	bear2 = game.obj_create( 14053, location_from_axis (427L, 447L) )
	#	bear2.rotation = 3
	#	bear2.npc_flag_set( ONF_KOS )
	#	boar1 = game.obj_create( 14507, location_from_axis (485L, 502L) )
	#	boar1.rotation = 2
	#	boar1.npc_flag_set( ONF_KOS )
	#	boar2 = game.obj_create( 14520, location_from_axis (507L, 502L) )
	#	boar1.rotation = 3
	#	boar2.npc_flag_set( ONF_KOS )
	#	boar3 = game.obj_create( 14521, location_from_axis (506L, 505L) )
	#	boar1.rotation = 2
	#	boar3.npc_flag_set( ONF_KOS )
	#	# Look for Bandits
	#	bandit1 = find_npc_near(attachee,14070)
	#	if (bandit1 == OBJ_HANDLE_NULL):
	#		# Bandits dead.  Create Skeletons
	#		skell1 = game.obj_create( 14600, location_from_axis (468L, 451L) )
	#		skell1.concealed_set(1)
	#		skell2 = game.obj_create( 14602, location_from_axis (465L, 452L) )
	#		skell2.concealed_set(1)
	#		skell7 = game.obj_create( 14601, location_from_axis (465L, 452L) )
	#		skell7.concealed_set(1)
	#		skell3 = game.obj_create( 14603, location_from_axis (464L, 457L) )
	#		skell3.concealed_set(1)
	#		skell4 = game.obj_create( 14600, location_from_axis (473L, 447L) )
	#		skell4.concealed_set(1)
	#		skell5 = game.obj_create( 14602, location_from_axis (479L, 447L) )
	#		skell5.concealed_set(1)
	#		skell6 = game.obj_create( 14603, location_from_axis (463L, 475L) )
	#		skell6.concealed_set(1)
	#		zomb1 = game.obj_create( 14615, location_from_axis (489L, 450L) )
	#		zomb1.concealed_set(1)
	#		zomb2 = game.obj_create( 14615, location_from_axis (480L, 448L) )
	#		zomb2.concealed_set(1)
	#		zomb3 = game.obj_create( 14615, location_from_axis (463L, 462L) )
	#		zomb3.concealed_set(1)
	#	attachee.destroy()
	#elif (attachee.map == 5004): # Interior of Moathouse
	#	# Create Basilisk and Statue
	#	bas = game.obj_create( 14295, location_from_axis (499L, 471L) )		##basilisk
	#	bas.npc_flag_set( ONF_KOS )
	#	bas.turn_towards(game.party[0])
	#	game.obj_create( 2050, location_from_axis (487L, 480L) )
	#	game.obj_create( 2051, location_from_axis (512L, 478L) )
	#	statue = game.obj_create( 2048, location_from_axis (494L, 484L) )	##statue
	#	statue.rotation = 5
	#	game.obj_create( 14142, location_from_axis (511L, 474L) )		##ooze
	#	# Look for Snake
	#	snake = find_npc_near(attachee,14088)
	#	if (snake == OBJ_HANDLE_NULL):
	#		# Snake dead.  Create Werewolves, Jackalweres and Wolf
	#		game.obj_create( 14344, location_from_axis (469L, 510L) )
	#		game.obj_create( 14138, location_from_axis (478L, 509L) )
	#		wolf = game.obj_create( 14391, location_from_axis (484L, 513L) )
	#		wolf.turn_towards(game.party[0])
	#		wolf.npc_flag_set( ONF_KOS )
	#		game.obj_create( 14138, location_from_axis (477L, 514L) )
	#		game.obj_create( 14344, location_from_axis (468L, 509L) )
	#	attachee.destroy()
	dummy = 1	
	#elif (attachee.map == 5005): # Moathouse Dungeon
	#	if game.global_vars[52] >= 1:			# spawn witch and cohorts 
	#		game.obj_create( 14680, location_from_axis (481L, 532L) )	# witch
	#		game.obj_create( 14110, location_from_axis (488L, 533L) )	# quasit
	#		game.obj_create( 14110, location_from_axis (478L, 540L) )	# quasit
	#		game.obj_create( 14125, location_from_axis (496L, 535L) )	# zombie
	#		game.obj_create( 14130, location_from_axis (493L, 530L) )	# lacedon
	#		game.obj_create( 14137, location_from_axis (492L, 534L) )	# ghast
	#		game.obj_create( 14328, location_from_axis (488L, 529L) )	# bodak
	#		game.obj_create( 14137, location_from_axis (488L, 545L) )	# ghast
	#		game.obj_create( 14130, location_from_axis (490L, 530L) )	# lacedon
	#		game.obj_create( 14125, location_from_axis (490L, 530L) )	# zombie
	#		game.obj_create( 14141, location_from_axis (421L, 440L) )	# ooze
	#		game.obj_create( 14141, location_from_axis (409L, 438L) )	# ooze
	#		skelp = game.obj_create( 14601, location_from_axis (492L, 533L) )
	#		skelp.concealed_set(1)
	#		skelb1 = game.obj_create( 14603, location_from_axis (482L, 537L) )
	#		skelb1.concealed_set(1)
	#		skelb2 = game.obj_create( 14603, location_from_axis (473L, 536L) )
	#		skelb2.concealed_set(1)
	#		skelb3 = game.obj_create( 14603, location_from_axis (473L, 538L) )
	#		skelb3.concealed_set(1)
	#		game.global_vars[52] = 0
		
	#	# Check to see which spawner this is.
	#	y = (attachee.critter_flags_get() & OCF_MUTE)
	#	if y == 0:
	#		# The non-mute spawner.  Look for Bugbears.
	#		bugbear2 = find_npc_near(attachee,14172)
	#		if (bugbear2 == OBJ_HANDLE_NULL):
	#			# Bugbears dead.  Spawn Groaning Spirit and others.
	#			game.obj_create( 14280, location_from_axis (429L, 516L) )
	#			game.obj_create( 14289, location_from_axis (420L, 518L) )	# shadow
	#			game.obj_create( 14289, location_from_axis (419L, 527L) )	# shadow
	#			game.obj_create( 14328, location_from_axis (418L, 525L) )	# bodak
	#			game.obj_create( 14289, location_from_axis (428L, 532L) )	# shadow
	#			game.obj_create( 14289, location_from_axis (430L, 520L) )	# shadow
	#			game.obj_create( 14328, location_from_axis (423L, 515L) )	# bodak
	#		attachee.destroy()
	#		
	#	else:
	#		# The mute spawner.  Look for Gnolls
	#		gnolls1 = find_npc_near(attachee,14079)
	#		gnolls2 = find_npc_near(attachee,14066)
	#		if ((gnolls1 == OBJ_HANDLE_NULL) and (gnolls2 == OBJ_HANDLE_NULL) and game.global_flags[288] != 1):
	#			# Gnolls dead.  Spawn skeletal gnolls
	#			game.obj_create( 14081, location_from_axis (482L, 468L) )
	#			game.obj_create( 14082, location_from_axis (483L, 477L) )
	#			game.obj_create( 14083, location_from_axis (482L, 473L) )
	#			game.obj_create( 14616, location_from_axis (484L, 502L) )
	#			game.obj_create( 14081, location_from_axis (483L, 481L) )
	#			game.obj_create( 14082, location_from_axis (482L, 475L) )
	#			game.obj_create( 14083, location_from_axis (491L, 485L) )
	#			game.obj_create( 14616, location_from_axis (486L, 486L) )
	#		attachee.destroy()
	dummy = 1
	return 

def Council_Script(attachee, triggerer):
	if (attachee.map == 5048 and game.global_flags[432] == 0):
		if (get_v(435) == 3):
		#full council assembly spawn
			burne = game.obj_create( 14004, location_from_axis (477, 470) )
			burne.move( location_from_axis (477, 470),0.0,15.0)
			burne.rotation = 2.3
			burne.obj_set_int(obj_f_critter_description_unknown,20000)
			destroy_weapons(burne,4058,0,0)
			rufus = game.obj_create( 14006, location_from_axis (474, 472) )
			rufus.move(location_from_axis (474, 472),15.0,-10.0 )
			rufus.rotation = 2.5
			rufus.obj_set_int(obj_f_critter_description_unknown,20002)
			jaroo = game.obj_create( 14005, location_from_axis (474, 476) )
			jaroo.rotation = 5.5
			destroy_weapons(jaroo,4047,4111,0)
			jaroo.obj_set_int(obj_f_critter_description_unknown,20001)
			renton = game.obj_create( 14012, location_from_axis (477, 475) )
			renton.move(location_from_axis (477, 475),0.0,-8.0 )
			renton.rotation = 5.4
			terjon = game.obj_create( 14007, location_from_axis (480, 474) )
			terjon.move(location_from_axis (480, 474),-25.0,0.0 )
			terjon.rotation = 5.8
			destroy_weapons(terjon,4124,6054,0)
			badger1 = game.obj_create( 14371, location_from_axis (482, 474) )
			badger1.move(location_from_axis (482, 474),0.0,0.0 )
			badger1.rotation = 2.1
			nevets = game.obj_create(14102,location_from_axis(475,477))
			nevets.move(location_from_axis(475,477),-8.0,0.0)
			nevets.rotation = 5.2
			miller = game.obj_create(14031,location_from_axis(477,477))
			miller.move(location_from_axis(477,477),3.0,0.0)
			miller.rotation = 5.3
			gundi = game.obj_create(14016,location_from_axis(479,477))
			gundi.move(location_from_axis(479,477),0.0,0.0)
			gundi.rotation = 5.8

			game.global_flags[432] = 1
			game.leader.begin_dialog(burne, 7000)
		elif (get_v(435) == 2):
		#only badgers spawn, if you suspected R&G
			badger1 = game.obj_create( 14371, location_from_axis (476, 477) )
			badger1.rotation = 2.5
			badger2 = game.obj_create( 14371, location_from_axis (479, 477) )
			badger2.rotation = 2.6
			badger3 = game.obj_create( 14371, location_from_axis (474, 476) )
			badger3.rotation = 2.5
			game.global_flags[432] = 1
	elif (attachee.map == 5048 and get_v(435) >= 4):
		#this should delete everyone after it's all over
		for npc in game.obj_list_vicinity(attachee.location,OLC_NPC):
			if (to_be_deleted(npc) == 1 and npc.leader_get() == OBJ_HANDLE_NULL):
				npc.destroy()
	return

def to_be_deleted(npc):
	#8008 - Gundigoot
	#8054 - Burne
	#14031 - Miller
	#14102 - Nevets
	#14371 - badger
	#20001 - Jaroo
	#20003 - Terjon
	#20007 - Renton
	if (npc.name == 8008 or npc.name == 8048 or npc.name == 8049 or npc.name == 8054 or npc.name == 14006 or npc.name == 14031 or npc.name == 14102 or npc.name == 14806 or npc.name == 14371 or npc.name == 20001 or npc.name == 20003 or npc.name == 20007):
		return 1
	return 0

def destroy_weapons(npc, item1, item2, item3):
	if (item1 != 0):
		moshe = npc.item_find(item1)
		if (moshe != OBJ_HANDLE_NULL):
			moshe.destroy()
	if (item2 != 0):
		moshe = npc.item_find(item2)
		if (moshe != OBJ_HANDLE_NULL):
			moshe.destroy()
	if (item3 != 0):
		moshe = npc.item_find(item3)
		if (moshe != OBJ_HANDLE_NULL):
			moshe.destroy()
	return
